<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="uname">
        <span style="color: rgba(0, 145, 255, 1)">账号：{{ uphonesss }}</span>
      </el-form-item>
      <el-form-item label="" prop="uname">
        <el-date-picker
          v-model="timewww"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:miniuser:export']"
        >导出</el-button>
        <el-button icon="el-icon-back" size="mini" @click="bank">返回</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      
    </el-row>
    
    <el-table v-loading="loading"  :data="miniuserList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="序号" align="center" prop="uname" show-overflow-tooltip /> -->
      <el-table-column label="编辑时间" align="center" prop="updateTime" show-overflow-tooltip />
      <el-table-column label="姓名" align="center" prop="uname" show-overflow-tooltip />
      <el-table-column label="身份证号" align="center" prop="card" show-overflow-tooltip />
      <el-table-column label="出生年月" align="center" prop="birthData" show-overflow-tooltip />
      <el-table-column label="性别" align="center" prop="sex" show-overflow-tooltip />
      <el-table-column label="年龄" align="center" min-width="100" prop="age" show-overflow-tooltip />
      <el-table-column label="身高（cm）" align="center" min-width="100" prop="height" show-overflow-tooltip />
      <!-- <el-table-column label="性别" align="center" prop="sex" /> -->
      <el-table-column label="体重（kg）" align="center" prop="weight" show-overflow-tooltip />
      <el-table-column label="住址" align="center" prop="address" show-overflow-tooltip />
      <el-table-column label="最近就医" align="center" prop="medicalName" show-overflow-tooltip />
      <el-table-column label="就医原因" align="center" prop="medicalReasonsName" show-overflow-tooltip />
      <el-table-column label="最近服用药" align="center" min-width="100" prop="recentMedication" show-overflow-tooltip />
      <el-table-column label="药物类别" align="center" prop="drugCategoryName"  show-overflow-tooltip />
      <el-table-column label="药物名称" align="center" prop="drugName" show-overflow-tooltip />
      <el-table-column label="亲属姓名" align="center" prop="relativesName" show-overflow-tooltip />
      <el-table-column label="亲属电话" align="center" prop="relativesPhone" show-overflow-tooltip />
      <el-table-column label="关系" align="center" prop="relationshipName" show-overflow-tooltip />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="uname">
          <el-input v-model="form.uname" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="手机号" prop="uphone">
          <el-input v-model="form.uphone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>
        <el-form-item label="unionId" prop="unionid">
          <el-input v-model="form.unionid" placeholder="请输入unionId" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthData">
          <el-input v-model="form.birthData" placeholder="请输入出生日期" />
        </el-form-item>
        <el-form-item label="身份证" prop="card">
          <el-input v-model="form.card" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="身高" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="最近就医id" prop="medicalId">
          <el-input v-model="form.medicalId" placeholder="请输入最近就医id" />
        </el-form-item>
        <el-form-item label="医疗原因" prop="medicalReasons">
          <el-input v-model="form.medicalReasons" placeholder="请输入医疗原因" />
        </el-form-item>
        <el-form-item label="近期用药" prop="recentMedication">
          <el-input v-model="form.recentMedication" placeholder="请输入近期用药" />
        </el-form-item>
        <el-form-item label="药物频率id" prop="drugFrequencyId">
          <el-input v-model="form.drugFrequencyId" placeholder="请输入药物频率id" />
        </el-form-item>
        <el-form-item label="药物类别_id" prop="drugCategoryId">
          <el-input v-model="form.drugCategoryId" placeholder="请输入药物类别_id" />
        </el-form-item>
        <el-form-item label="药物名称" prop="drugName">
          <el-input v-model="form.drugName" placeholder="请输入药物名称" />
        </el-form-item>
        <el-form-item label="亲属id(user表的user_id)" prop="detailsId">
          <el-input v-model="form.detailsId" placeholder="请输入亲属id(user表的user_id)" />
        </el-form-item>
        <el-form-item label="关系id" prop="relationshipId">
          <el-input v-model="form.relationshipId" placeholder="请输入关系id" />
        </el-form-item>
        <el-form-item label="用户地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入用户地址" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable
            v-model="form.createdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedTime">
          <el-date-picker clearable
            v-model="form.updatedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户所在国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入用户所在国家" />
        </el-form-item>
        <el-form-item label="用户所在省份 " prop="province">
          <el-input v-model="form.province" placeholder="请输入用户所在省份 " />
        </el-form-item>
        <el-form-item label=" 用户所在城市 " prop="city">
          <el-input v-model="form.city" placeholder="请输入 用户所在城市 " />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMiniuser, getMiniuser, delMiniuser, addMiniuser, updateMiniuser, getregion, getlishilist } from "@/api/system/miniuser";

export default {
  name: "Miniuser",
  data() {
    return {
      props: {
        lazy: true,
        lazyLoad (node, resolve) {
            // console.log(node)
          const { level } = node;
          let data = []
          // setTimeout(() => {
          let id = 0
          // console.log(level)
          if(level != 0) {
              id = node.value
          }
          getregion(id).then(res => {
              res.rows.map(item => {
                  // console.log(item)
                  let obj = {
                    value: item.id,
                    label: item.name,
                    // leaf: level >= 3
                  }
                  // console.log(obj)
                  data.push(obj)
              })
              resolve(data);
          });
          // }, 1000);
            // 通过调用resolve将子节点数据返回，通知组件数据加载完成
        }
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户基础信息表格数据
      miniuserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        addressList: [],
        uphone: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      order: '',
      querysele: '',
      options: [],
      uphonesss: '',
      timewww: []
    };
  },
  created() {
    // console.log(this.$route.query)
    this.id = this.$route.query.id
    this.uphonesss = this.$route.query.uphone
    this.queryParams.uphone = this.$route.query.uphone
    this.getList();
  },
  methods: {
    /** 查询用户基础信息列表 */
    getList() {
      this.loading = true;
      const ordername = this.order
      let parse = {
        ...this.queryParams,
      }
      parse[ordername] = this.querysele
      getlishilist(parse).then(response => {
        // response.rows.forEach(item => {
        //   if(item.sex == '0'){
        //     item.sex = '男'
        //   } else if(item.sex == '1'){
        //     item.sex = '女'
        //   } else if(item.sex == '2'){
        //     item.sex = '未知'
        //   }
        // })
        this.miniuserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 返回
    bank() {
        this.$router.push({ 
            path: '/miniuser',
            // query: {
            //     id: row.id
            // }
        })
    },
    // 详情
    handledet(row) {
      // this.open = true
      // console.log(row)
      this.$router.push({ 
        path: '/userdet',
        query: {
          id: row.id
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uname: null,
        uphone: null,
        password: null,
        openid: null,
        unionid: null,
        avatar: null,
        utype: null,
        sex: null,
        birthData: null,
        card: null,
        age: null,
        height: null,
        weight: null,
        medicalId: null,
        medicalReasons: null,
        recentMedication: null,
        drugFrequencyId: null,
        drugCategoryId: null,
        drugName: null,
        detailsId: null,
        relationshipId: null,
        address: null,
        createdTime: null,
        updatedTime: null,
        longitude: null,
        latitude: null,
        nickName: null,
        country: null,
        province: null,
        city: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.startTime = this.timewww[0]
      this.queryParams.endTime = this.timewww[1]
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        addressList: []
      }
      this.queryParams.uphone = this.$route.query.uphone
      this.timewww = []
      this.queryParams.startTime = ''
      this.queryParams.endTime = ''
      this.querysele = ''
      this.order = ''
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMiniuser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMiniuser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMiniuser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户基础信息编号为"' + ids + '"的数据项？').then(function() {
        return delMiniuser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/miniuser/export', {
        ...this.queryParams
      }, `miniuser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
